export default {
    init(callback) {
        const {performance} = window
        let isDOMReady = false
        let isOnload = false
        const cycleTime = 100
        const Utils = {
            // DOM 解析完成，此时 laodEvent 修改的数据有误
            domReady(cb){
                if(isDOMReady){return}
                if(document.readyState === 'interactive'){
                    cb()
                    return
                }
                const check = ()=>{
                    let timer = null
                    if(performance.timing.domComplete){
                        clearTimeout(timer)
                        cb()
                        isDOMReady = true
                        return
                    }
                    timer = setTimeout(check,cycleTime)
                }
                document.addEventListener('DOMContentLoaded',()=>{
                    check()
                })
             },
            // 页面加载完成
            onload(cb){
                if(isOnload){return}
                if(document.readyState === 'interactive'){
                    cb()
                    return
                }
                const check = ()=>{
                    let timer = null
                    if(performance.timing.loadEventEnd){
                        clearTimeout(timer)
                        cb()
                        isOnload = true
                        return
                    }
                    timer = setTimeout(check,cycleTime)
                }
                window.addEventListener('load',()=>{
                    check()
                })
            },
            getPerformanceData(t){
                return {
                    // 建立连接
                    prePage: t.fetchStart - t.navigationStart, // 上一个页面时间
                    redierct: t.redirectEnd - t.redirectStart, // 重定向时间
                    dns:t.domainLookupEnd - t.domainLookupStart, // DNS 加载
                    connect:t.connectEnd - t.connectStart, // TCP 握手
                    network:t.connectEnd - t.navigationStart, // 网络总耗时 
                    // 网络
                    send: t.requestStart - t.requestStart, // 发送到接收时间
                    receive:t.responseEnd - t.responseStart, // 接收数据耗时
                    request:t.responseEnd - t.requestStart, // 请求页面总耗时
                    // 渲染
                    dom:t.domComplete - t.domLoading, // DOM 解析时间
                    loadEvent:t.loadEventEnd - t.loadEventStart, // load event
                    frontend:t.loadEventEnd - t.domLoading, // 前端总耗时
                    // 关键阶段
                    load: t.loadEventEnd - t.navigationStart, // 页面完全加载时间
                    domReady: t.domContentLoadedEventStart -  t.navigationStart, // dom 准备时间
                    interactive:t.domInteractive - t.navigationStart, // 可操作时间
                    ttfb:t.responseStart - t.navigationStart, // 首字节时间
                }
            }
        }
         
       Utils.domReady(()=>{
         let performanceData = Utils.getPerformanceData(performance.timing)
         performanceData.type="domready"
         callback(performanceData)
       })
       Utils.onload(()=>{
         let performanceData = Utils.getPerformanceData(performance.timing)
         performanceData.type="onload"
         callback(performanceData)
       })
    },
    
}